/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.client.http.AuthSubUtil;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.Name;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import properties.PropertyHolder;

/**
 *
 * @author dzaric
 */
public class ImportFromGmail extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
			if(request.getParameter("token")==null){
				String domain=PropertyHolder.getProperty("BASE_URL");
				String next = domain+"user/gmailimport.faces";
				System.out.println(next);
				String scope = "https://www.google.com/m8/feeds/";
				boolean secure = false;
				boolean session = true;
				String authSubLogin = AuthSubUtil.getRequestUrl(next, scope, secure, session);
				response.sendRedirect(authSubLogin);
			}else{
				String sessionToken=(String)request.getParameter("token");
				ContactsService myService=new ContactsService("Moja aplikacija");
				myService.setAuthSubToken(sessionToken);
				// Request the feed
				  URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full?max-results=1000");
				  ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);
				  // Print the results
				  System.out.println(resultFeed.getTitle().getPlainText());
				  for (int i = 0; i < resultFeed.getEntries().size(); i++) {
					ContactEntry entry = resultFeed.getEntries().get(i);
					out.print("<b>");
					out.print("Name: ");
					if (entry.hasName()) {
					  Name name = entry.getName();
					  out.println(name.getFullName().getValue());
					}else{
						out.println("N/A");
					}
					out.print("</b>");
					out.println("<br/>&nbsp&nbsp&nbsp");
					for (Email email : entry.getEmailAddresses()) {
					  out.print(" " + email.getAddress());
					}
					out.print("<br/>");
				  }	
			}
        }
		catch(Exception e){
			e.printStackTrace();
		}
		finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
